package org.zjut.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zjut.db.CourseDataBase;
import org.zjut.util.CommonUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Rising
 * @date 2017/9/14
 * @des 课程相关操作
 */
@Controller
public class CourseController {
    @Autowired
    CourseDataBase courseDataBase;

    /**
     * 增加课程
     * @param request
     * @return
     */
    @RequestMapping(value = "/addCourse", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addCourse(HttpServletRequest request){
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        return JSON.toJSONString(courseDataBase.addCourse(JSON.parseObject(CommonUtil.getBodyRequest(request))));
    }

    /**
     * 删除课程
     * @param request
     * @return
     */
    @RequestMapping(value = "/delCourse", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delCourse(HttpServletRequest request){
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        return JSON.toJSONString(courseDataBase.delCourse(JSON.parseObject(CommonUtil.getBodyRequest(request))));
    }


    /**
     * 编辑课程
     * @param request
     * @return
     */
    @RequestMapping(value = "/editCourse", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String editCourse(HttpServletRequest request){
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        return JSON.toJSONString(courseDataBase.editCourse(JSON.parseObject(CommonUtil.getBodyRequest(request))));
    }

    /**
     * 查询课程
     * @param request
     * @return
     */
    @RequestMapping(value = "/getCourse", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getCourse(HttpServletRequest request){
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }
        return JSON.toJSONString(courseDataBase.getCourse(JSON.parseObject(CommonUtil.getBodyRequest(request))));
    }


}
